/**
 * Copyright (C) 2012 The SkyTvOS Project
 *
 * Version     Date           Author
 * ─────────────────────────────────────
 *           2012-10-15         luwei
 *
 */

package com.skyworth.util;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPool
{
    private static ExecutorService threadPoolManager = null;
    
    public static void execute(Runnable task)
    {
        generateThreadPool();
        if (task != null)
        {
            threadPoolManager.execute(task);
        }
    }

    public static void executeDelay(final Runnable task, final long delay){
        ThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(delay);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ThreadPool.execute(task);
            }
        });
    }

    public static synchronized Executor getInstance(){
        generateThreadPool();
        return threadPoolManager;
    }

    
    private static void generateThreadPool()
    {
        if (threadPoolManager == null)
        {
            threadPoolManager = Executors.newCachedThreadPool();
        }
    }

    
    public static void shutDownThreadPool()
    {
        if (threadPoolManager != null)
        {
            threadPoolManager.shutdown();
            threadPoolManager = null;
        }
    }


}
